import { Controller, Get, Post, Body, Query, Request, Param } from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { CreateAuthDto } from './dto/create-auth.dto';
@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post('save')
  async create(@Body() createRoleDto: CreateRoleDto) {
    const result = await this.roleService.create(createRoleDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('load')
  async findAll(@Query() Query: any) {
    const [data, total] = await this.roleService.findAll(Query);
    return {
      data,
      total
    }
  }

  @Get('delete/:id')
  async delete(@Param('id') id: string) {
    const result = await this.roleService.delete(id);
    if(result){
      return {
        message: result ? '删除成功': '失败'
      }
    }
  }

  @Get('getRoleAuth')
  async getRoleAuth(@Query() query: any) {
    const result = await this.roleService.getRoleAuth(query);
    if(result){
      return {
        data: result
      }
    }
  }

  @Get('getRoleAuthList')
  async getRoleAuthList(@Query() query: any) {
    const result = await this.roleService.getRoleAuthList(query);
    if(result){
      return {
        data: result
      }
    }
  }

  @Post('saveRoleAuth')
  async saveRoleAuth(@Body() createAuthDto: CreateAuthDto) {
    const result = await this.roleService.saveRoleAuth(createAuthDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('getMenuAuthByAdmin/:route')
  async getMenuAuthByAdmin(@Request() req:any, @Param('route') route: string) {
    const result = await this.roleService.getMenuAuthByAdmin(route, req.tokenInfo.id);
    if(result){
      return {
        data: result
      }
    }
  }
}
